package com.ls.shiro;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;

/**
 * <p></p>
 * Created by zhezhiyong@163.com on 2017/2/28.
 */
//@Component
public class RedisSessionDao extends AbstractSessionDAO {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    protected Serializable doCreate(Session session) {
        Serializable sessionId = this.generateSessionId(session);
        this.assignSessionId(session, sessionId);
        redisTemplate.opsForValue().set(sessionId.toString(), session);
        return sessionId;
    }

    @Override
    protected Session doReadSession(Serializable serializable) {
        Session session = (Session) redisTemplate.opsForValue().get(serializable.toString());
        return session;
    }

    @Override
    public void update(Session session) throws UnknownSessionException {
        redisTemplate.opsForValue().set(session.getId().toString(), session);
    }

    @Override
    public void delete(Session session) {
        redisTemplate.delete(session.getId().toString());
    }

    @Override
    public Collection<Session> getActiveSessions() {
        throw new UnsupportedOperationException("getActiveSessions");
    }
}
